<?php
/**
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * This license is also available via the world-wide-web at
 * http://itdapps.ncsu.edu/bsd.txt
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to itappdev@ncsu.edu so we can send you a copy immediately.
 *
 * @package    Docs
 * @copyright  Copyright (c) 2007 NC State University Information Technology Division
 * @license    http://itdapps.ncsu.edu/bsd.txt  BSD License
 * @author     Jason Austin <jason_austin@ncsu.edu>
 * @author     Garrison Locke <garrison_locke@ncsu.edu>
 * @see        http://itdapps.ncsu.edu
 * @version    SVN: $Id: ActionLog.php 155 2007-07-19 19:44:26Z jfaustin@EOS.NCSU.EDU $
 */

/**
 * 
 * @package    Docs
 * @category   Model
 * @copyright  Copyright (c) 2007 NC State University Information Technology Division
 *
 */
class Gdata_Apps_Docs 
{
    const DOCS_LIST_URI = 'http://docs.google.com/feeds/default/private/full';
    const DOCS_DOWNLOAD_URI = 'http://docs.google.com/feeds/download/documents/Export';
    const SPREADSHEET_DOWNLOAD_URI = 'http://spreadsheets.google.com/feeds/download/spreadsheets/Export';
    const PRESENTATION_DOWNLOAD_URI = 'http://docs.google.com/feeds/download/presentations/Export';
    
    protected $_domain = null;
    protected $_username = null;
    protected $_password = null;
    protected $_gdataClient = null;    
    
    public function __construct()
    {
        $this->_domain = Zend_Registry::get('gdataDomain');
        $this->_username = Zend_Registry::get('gdataUsername');
        $this->_password = Zend_Registry::get('gdataPassword');
    }
    
    public function getDocsList($username)
    {        
        $request = new Google_Gdata_Request();
        $request->setUrl(self::DOCS_LIST_URI)
                ->setMethod(Google_Gdata_Request::GET)
                ->setConsumerKey(Zend_Registry::get('gdataOauthKey'))
                ->setConsumerSecret(Zend_Registry::get('gdataOauthSecret'))
                ->setRequestorId($username . '@' . $this->_domain);
                
        $feed = $request->send();
        
        $feed = simplexml_load_string($feed);        
        
        $docList = array();
        
        foreach ($feed->entry as $entry) {
           
            $contentLink = $entry->content->attributes()->src;
            $parts = parse_url($contentLink);
            $query = explode('&', $parts['query']);
            $query[0] = explode('=', $query[0]);           
            $docId = $query[0][1];
          
            if (strstr($contentLink, 'presentations')) {
                $type = 'presentation';
            } else if (strstr( $contentLink, 'documents')) {
                $type = 'document';
            } else if (strstr( $contentLink, 'spreadsheets')) {
                $type = 'spreadsheet';
            } else {
                $type = 'document';
            }
                        
            $docList[] = array(
                            'title' => (string)$entry->title,
                            'user'  => $username,
                            'docId' => $docId,
                            'type'  => $type 
                         );
        }
        
        return $docList;
    }
    
    public function download($username, $docId, $type, $format = null)
    {
        switch ($type) {
            
            case 'document' :
                $result = $this->_downloadDocument($username, $docId, $format);
                break;
            case 'presentation' :
                $result = $this->_downloadPresentation($username, $docId, $format);
                break;
            case 'spreadsheet' :
                $result = $this->_downloadSpreadsheet($username, $docId, $format); 
                break;
        }
        
        return $result;
    }

    /**
     * @param username The username of the person owning the presentation
     * @param docId The document ID to download
     * @param format Must be one of pdf, png, ppt, swf, or txt
     */
    protected function _downloadPresentation($username, $docId, $format = null)
    {   
        if (is_null($format)) {
            $format = 'ppt';
        }        
        
        $request = new Google_Gdata_Request();
        $request->setUrl(self::DOCS_DOWNLOAD_URI)
                ->setMethod(Google_Gdata_Request::GET)
                ->setParams(array('exportFormat' => $format, 'docID' => $docId))
                ->setConsumerKey(Zend_Registry::get('gdataOauthKey'))
                ->setConsumerSecret(Zend_Registry::get('gdataOauthSecret'))
                ->setRequestorId($username . '@' . $this->_domain);
                
        $result = $request->send();
               
        return array('contents' => $result, 'extension' => $format);
    }
    
    /**
     * @param username The username of the person owning the document
     * @param docId The document ID to download
     * @param format Must be one of doc, html, odt, pdf, png, rtf, txt, or zip
     */
    protected function _downloadDocument($username, $docId, $format = null)
    {           
        if (is_null($format)) {
            $format = 'doc';
        }        
        
        $request = new Google_Gdata_Request();
        $request->setUrl(self::DOCS_DOWNLOAD_URI)
                ->setMethod(Google_Gdata_Request::GET)
                ->setParams(array('exportFormat' => $format, 'docID' => $docId))
                ->setConsumerKey(Zend_Registry::get('gdataOauthKey'))
                ->setConsumerSecret(Zend_Registry::get('gdataOauthSecret'))
                ->setRequestorId($username . '@' . $this->_domain);
                
        $result = $request->send();
               
        return array('contents' => $result, 'extension' => $format);
    }
    
    /**
     * @param username The username of the person owning the spreadsheet
     * @param docId The document ID to download
     * @param format Must be one of xls, csv, pdf, ods, tsv, or html
     */
    protected function _downloadSpreadsheet($username, $docId, $format = null)
    {   
        if (is_null($format)) {
            $format = 'xls';
        }                
        
        $request = new Google_Gdata_Request();
        $request->setUrl(self::SPREADSHEET_DOWNLOAD_URI)
                ->setMethod(Google_Gdata_Request::GET)
                ->setParams(array('exportFormat' => $format, 'key' => $docId))
                ->setConsumerKey(Zend_Registry::get('gdataOauthKey'))
                ->setConsumerSecret(Zend_Registry::get('gdataOauthSecret'))
                ->setRequestorId($username . '@' . $this->_domain);
                
        $result = $request->send();
               
        return array('contents' => $result, 'extension' => $format);
    }    
}